package vip.xiaonuo.smzq.modular.nbfb.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.excel.CommonExcelCustomMergeStrategy;
import vip.xiaonuo.common.util.CommonResponseUtil;
import vip.xiaonuo.smzq.core.tools.file.PathUtils;
import vip.xiaonuo.smzq.modular.kcl.entity.Zycl;
import vip.xiaonuo.smzq.modular.nbfb.entity.vo.NbfbFiveVo;
import vip.xiaonuo.smzq.modular.nbfb.entity.vo.NbfbOneVo;
import vip.xiaonuo.smzq.modular.nbfb.entity.vo.NbfbThreeVo;
import vip.xiaonuo.smzq.modular.nbfb.mapper.ZyclOneMapper;
import vip.xiaonuo.smzq.modular.nbfb.service.INbfbOneService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NbfbOneServiceImpl extends ServiceImpl<ZyclOneMapper, Zycl> implements INbfbOneService {

    @Resource
    private ZyclOneMapper zyclOneMapper;

    @Value("${file.base}")
    String baseFilePath;

    /**
     * 年报附表1查询
     *
     * @param nd
     * @param kclb
     * @author Kevin
     * @since 2024/7/30 21:27
     */
    @Override
    public List<NbfbOneVo> getNbfbOneList(Integer nd, String kclb) {
      String sql=" and ks.nd = "+nd;
       /* if (StrUtil.isNotBlank(kclb)) {
            sql+=" and  zh.dm = '" + kclb + "'";
        }*/
        return zyclOneMapper.getNbfbOneListNew( nd);
    }

    /**
     * 导出年报附表1
     *
     * @param nd
     * @param kclb
     * @param response
     * @author Kevin
     * @since 2024/7/31 9:54
     */
    @Override
    public void exportNbfbOne(Integer nd, String kclb, HttpServletResponse response) {
        exportNbfbOneTemplateExport(nd,kclb,response);
    }

    /**
     * 根据模版导出年报附表1
     * @author Sqq
     * @param nd
     * @param kclb
     * @param response
     */
    public void exportNbfbOneTemplateExport(Integer nd, String kclb, HttpServletResponse response) {
        //年报附表1模板路径
        String templateFilePath = PathUtils.getNbfbPath(1);
        System.out.println("文件路径：：："+templateFilePath);
        //临时导出文件
        File tempFile = null;

        // 加载模板文件
        ClassPathResource classPathResource = new ClassPathResource(templateFilePath);
        InputStream inputStream = null;

        try {
            // 从资源路径获取输入流
            inputStream = classPathResource.getStream();
            List<NbfbOneVo> list = getNbfbOneList(nd, kclb);
            String fileName = nd + "年报报表附表1.xlsx";
            tempFile = new File(PathUtils.getUpLoadPath(baseFilePath) + File.separator + fileName);

            try (ExcelWriter excelWriter = EasyExcel.write(tempFile.getPath())
                             .registerWriteHandler(new CommonExcelCustomMergeStrategy(list.stream().map(NbfbOneVo::getDlmc)
                             .collect(Collectors.toList()), 0))
                             .withTemplate(inputStream).build()
            ) {
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                // 使用 fill 方法填充数据
                excelWriter.fill(list, writeSheet);
            }
            // 将生成的文件发送给客户端
            IoUtil.write(response.getOutputStream(), true, FileUtil.readBytes(tempFile));
        } catch (Exception e) {
            // 处理具体的异常
            System.err.println("Error generating Excel file: " + e.getMessage());
            // 可以考虑使用日志框架记录错误信息
            // logger.error("Error generating Excel file: ", e);
        } finally {
            // 从资源路径获取输入流
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (tempFile != null) {
                FileUtil.del(tempFile);
            }
        }
    }













}
